package nl.hu.modellingengine.grid.client.activity;

import nl.hu.modellingengine.client.ClientFactory;
import nl.hu.modellingengine.grid.client.GridFactory;
import nl.hu.modellingengine.ribbon.client.places.ElementPositionChangePlace;
import nl.hu.modellingengine.shared.Element;
import nl.hu.modellingengine.shared.ElementType;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class GridElementPositionChangeActivity extends AbstractActivity {
	
	private ClientFactory clientFactory;
	private GridFactory gridFactory;
	private ElementPositionChangePlace place;
	
	public GridElementPositionChangeActivity(ElementPositionChangePlace place,ClientFactory clientFactory, GridFactory gridFactory){
		this.place = place;
		this.clientFactory = clientFactory;
		this.gridFactory = gridFactory;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		//TODO Check if element is at good position else destroy it and show notification to user.
		GWT.log("test2");
		
		boolean exist = false;
		for(Element element:gridFactory.getElements()){
			if(element.getStrId().equals(place.getId())){
				exist=true;
			}
		}
		if(!exist){
			Element e = new Element();
			e.setStrId(place.getId());
			// TODO make call to server to getElementType Object
			gridFactory.getElements().add(e);
			gridFactory.getGridView().initElement(e.getStrId());
		}
	}
	private AsyncCallback<ElementType> getElementType(String elementType, final Element e){
		return new AsyncCallback<ElementType>(){

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage());
			}

			@Override
			public void onSuccess(ElementType result) {
				e.setElementType(result);
			}
		};
	}

}
